package com.tool_stdy.IO;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.util.Iterator;

/**
 * Created by yan_li on 2017/1/8.
 */
public class NIOTestServer implements Runnable {

    private Selector selector;

    public NIOTestServer(int port) throws IOException {
        this.selector = Selector.open();

        ServerSocketChannel ssc = ServerSocketChannel.open();

        ssc.configureBlocking(false);
        ssc.bind(new InetSocketAddress(port));
        ssc.register(this.selector, SelectionKey.OP_ACCEPT);
        System.out.println("server start port:" + port);
    }

    @Override
    public void run() {
        while (true) {
            try {
                this.selector.select();
                Iterator<SelectionKey> keys = this.selector.selectedKeys().iterator();
                while(keys.hasNext()){

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
